package org.chatboleh.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.chatboleh.exception.EDMException;

public class ConnectionFactory {
	protected static Logger logger = Logger.getLogger(ConnectionFactory.class);
	private static String URI = "jdbc:hsqldb:hsql://localhost/edbdb2";
	private static String DB_USERNAME = "sa";
	private static String DB_PASSWORD = "";
	
	private static Connection conn;
	
	public static Connection getInstance() throws EDMException {
		if (conn == null) { 
//			logger.debug("Creating new connection.");
			try {
				Class.forName("org.hsqldb.jdbcDriver");
				conn = DriverManager.getConnection(URI, DB_USERNAME, DB_PASSWORD);
			} catch (SQLException e) {
				throw new EDMException(e);
			} catch (ClassNotFoundException e) {
				throw new EDMException(e);
			}
		} else {
//			logger.debug("Using existing connection.");
		}
//		if (conn.isClosed()){
//			conn.
//		}
		return conn;
	}
	
	public static void CloseConnection() throws EDMException {
		try {
			logger.debug("Closing connection.");
			conn.close();
		} catch (SQLException e) {
			throw new EDMException(e);
		}
		conn = null;
	}
}
